﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PRE.Catalog 
{
    public partial class FrmDaybu : PRE.Catalog.FrmBase
    {
        DAL.EntitesDAL.DaybuDAL _dal = new DAL.EntitesDAL.DaybuDAL(); 
        DAL.EntitesDAL.GiaovienDAL _dal2 = new DAL.EntitesDAL.GiaovienDAL();
        DAL.EntitesDAL.LophocDAL _dal3 = new DAL.EntitesDAL.LophocDAL();
        DAL.EntitesDAL.KhoahocDAL _dal4 = new DAL.EntitesDAL.KhoahocDAL();

        public FrmDaybu()
        {
            InitializeComponent();
        }


        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {
            ReadOnlyControl(false);
            ResetText();
            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected override void PerformCancel()
        {
            ChangeStatus();
            PerformRefresh();
            base.PerformCancel();
        }

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            var id1 = grvMain.GetFocusedRowCellValue("MaGVID") + "";
            var id2 = grvMain.GetFocusedRowCellValue("MalopID") + "";

            DialogResult result;
            var ok = _dal.Delete(id1, id2);
            result = MessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    MessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);

            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();
            ChangeStatus();
            ResetText();

          //  btnGoSearch.Visible = false;

            if (_dtb != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }



            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {
            BLL.EntitiesBLL.DaybuBLL _bll = new BLL.EntitiesBLL.DaybuBLL();

            string malop = Convert.ToString(cboLophoc.EditValue);
            DataTable _dt = _dal3.Select_Khoa(malop);

            DateTime _ngay = Convert.ToDateTime(dteNgayday.EditValue);
            int kq = _bll.CheckDate(_dt,_ngay);

            if (kq==1)
                MessageBox.Show("Ngày dạy bù không được nhỏ hơn ngày khai giảng khóa học !!!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
                if (kq==2)
                    MessageBox.Show("Ngày dạy bù không được lớn hơn ngày kết thúc khóa học !!!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {
                    if (IsAdd)
                    {
                        var o = new DAL.Entities.Daybu()
                        {
                            MaGVID = Convert.ToString(cboGiaovien.EditValue),
                            MalopID = Convert.ToString(cboLophoc.EditValue),
                            Ngayday = Convert.ToDateTime(dteNgayday.EditValue)
                        };

                        var oki = _dal.Insert(o);
                        if (oki)
                        {
                            //MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            PerformRefresh();
                            ChangeStatus(false);
                            PerformAdd();
                        }
                        else
                        {
                            MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            ChangeStatus(false);
                            ReadOnlyControl(false);
                        }
                    }

                    if (IsEdit)
                    {
                        var id1 = grvMain.GetFocusedRowCellValue("MaGVID") + "";
                        var id2 = grvMain.GetFocusedRowCellValue("MalopID") + "";

                        var o = new DAL.Entities.Daybu()
                        {
                            MaGVID = Convert.ToString(id1),
                            MalopID = Convert.ToString(id2),
                            Ngayday = Convert.ToDateTime(dteNgayday.EditValue)
                        };

                        var oki = _dal.Update(o);
                        if (oki)
                        {
                            // MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            PerformRefresh();
                            ChangeStatus();
                        }
                        else
                        {
                            MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            ChangeStatus(false);
                            ReadOnlyControl(false);
                        }
                    }
                }

            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {
            // cboGiaoVien.Reset();
            cboGiaovien.ItemIndex = -1;
            cboLophoc.ItemIndex = -1;
            dteNgayday.ResetText();

            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            cboGiaovien.DataBindings.Clear();
            cboLophoc.DataBindings.Clear();
            dteNgayday.DataBindings.Clear();

            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {

            cboGiaovien.DataBindings.Add("EditValue", _dtb, ".MaGVID");
            cboLophoc.DataBindings.Add("EditValue", _dtb, ".MalopID");
            dteNgayday.DataBindings.Add("EditValue", _dtb, ".Ngayday");

            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            //txtMaHV.Properties.ReadOnly = isReadOnly;
            cboGiaovien.Properties.ReadOnly = isReadOnly;
            cboLophoc.Properties.ReadOnly = isReadOnly;
            dteNgayday.Properties.ReadOnly = isReadOnly;

            grcMain.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        protected override void PerformSearch()
        {
            ChangeStatus(false);
            ReadOnlyControl(false);
            ResetText();
          //  btnGoSearch.Visible = true;

            grcMain.Enabled = true;

            base.PerformSearch();
        }

        /// <summary>
        /// Execute Search data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //private void btnGoSearch_Click(object sender, EventArgs e)
        //{
        //    var o = new DAL.Entities.Daybu()
        //    {
        //        MaGVID = Convert.ToString(cboGiaovien.EditValue),
        //        MalopID = Convert.ToString(cboLophoc.EditValue),
        //        Ngayday = Convert.ToDateTime(dteNgayday.EditValue)
        //    };

        //    grcMain.DataSource = _dal.Search(o);

        //}

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            _dtb = _dal.Select();

            DataTable _dtb2 = new DataTable();
            _dtb2 = _dal2.Select();  // select tb_giaovien

            DataTable tmp = _dal4.Select_Current(DateTime.Now);
            DataTable _dtb3 = _dal3.Select(tmp.Rows[0]["Makhoa"] + ""); // select tb_lophoc

            // Đổ dữ liệu vào cbobox
            cboGiaovien.Properties.DataSource = _dtb2;
            cboLophoc.Properties.DataSource = _dtb3;

            if (_dtb != null)
            {
                grcMain.DataSource = _dtb;
                //gridColumn2.BestFit(); // fit column                
            }

            base.LoadData();
        }
        #endregion


        private void dockPanel1_Click(object sender, EventArgs e)
        {

        }

        private void FrmDaybu_Load(object sender, EventArgs e)
        {
            PerformRefresh();
        }
    }
}
